package com.example.springbook.interceptor;

import com.example.springbook.constant.Constants;
import com.example.springbook.model.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        UserInfo userInfo=(UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
        if (userInfo==null || userInfo.getId()<=0){
            //用户未登录
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
